/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.account.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.account.domain.AccountId40;
import me.zhengjie.modules.account.domain.dto.AccountId40QueryCriteria;
import me.zhengjie.modules.account.mapper.AccountId40Mapper;
import me.zhengjie.modules.account.service.AccountId40Service;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.NPETool;
import me.zhengjie.tool.NetUtils;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author lzw
* @date 2025-06-11
**/
@Service
@RequiredArgsConstructor
public class AccountId40ServiceImpl extends ServiceImpl<AccountId40Mapper, AccountId40> implements AccountId40Service {

    private final AccountId40Mapper accountId40Mapper;
    private final SequenceService sequenceService;

    @Override
    public PageResult<AccountId40> queryAll(AccountId40QueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(accountId40Mapper.findAll(criteria, page));
    }

    @Override
    public List<AccountId40> queryAll(AccountId40QueryCriteria criteria){
        return accountId40Mapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AccountId40 resources) {

        Sequence sequence = new Sequence();
        sequence.setName("aaz415");

        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");
        String currentUsername = SecurityUtils.getCurrentUsername();
        resources.setAae011(currentUsername);
        resources.setAae036(new BigDecimal(yyyyMMddHHmmss));
        resources.setAab359("530111");
        resources.setAae100(Constant.AAE100_1_YX);
        resources.setAaz415(sequenceService.nextSequence(sequence));
        accountId40Mapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AccountId40 resources) {
        AccountId40 accountId40 = getById(resources.getAaz415());
        accountId40.copy(resources);
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");
        String currentUsername = SecurityUtils.getCurrentUsername();
        accountId40.setAae011(currentUsername);
        accountId40.setAae036(new BigDecimal(yyyyMMddHHmmss));
        accountId40.setAab359("530111");

        accountId40Mapper.updateById(accountId40);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Map> ids) {
        accountId40Mapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<AccountId40> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AccountId40 accountId40 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("个人编号", accountId40.getAac001());
            map.put("参保关系ID", accountId40.getAaz159());
            map.put("职业年金总额", accountId40.getAie074());
            map.put("记账金额", accountId40.getAae240());
            map.put("实账金额", accountId40.getAae252());
            map.put("补记的职业年金金额", accountId40.getAic290());
            map.put("改革前试点划转金额", accountId40.getAic005());
            map.put("企业年金金额", accountId40.getAae019());
            map.put("有效标识", accountId40.getAae100());
            map.put("备注", accountId40.getAae013());
            map.put("经办人", accountId40.getAae011());
            map.put("经办时间", accountId40.getAae036());
            map.put("经办机构", accountId40.getAab359());
            map.put("业务日志ID", accountId40.getAaz002());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<AccountId40> queryId40(Map map) {

        NPETool.isEmptyThrowException(map.get("aaz159"),"人员参保关系ID");
        QueryWrapper<AccountId40> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aaz159",map.get("aaz159"));
        queryWrapper.eq("aae100", Constant.AAE100_1_YX);
        return accountId40Mapper.selectList(queryWrapper);
    }
}
